# coding:utf-8
# auth.py

import gtk
import threading
import os

from account import Account

class Auth():
	def __init__(self,account=Account()):
		self.__connState=False
		self.account=account
		self.su= []
		if os.getuid() != 0:
			self.su += ['gksu','--']

	def set_state(self,state):
		self.__connState=state

	def set_account(self,account):
		self.account=account
	def get_account(self):
		return self.account

	def set_connState(self,state):
		self.__connState=state
	def get_connState(self):
		return self.__connState

	def auth(self,widget=None,conn=False):
		if not self.account.isValid():
			raise 'ACCOUNT_NOT_VALID',''
		if conn != self.get_connState():
			self.set_connState(conn)
			if conn : self.connect()
			else : self.disconnect()

	def connect(self,data=None):
		username = self.account.get_username()
		password = self.account.get_password()
		cmd = self.su + ["scripts/auth",username,password]
                print(" ".join(cmd))
		os.system(" ".join(cmd))
		raise 'AUTH_DONE',''


	def disconnect(self,data=None):
		cmd = self.su + ["killall","myxrgsu"]
                print(" ".join(cmd))
		os.system(" ".join(cmd))


auth = Auth()

if __name__ == "__main__":
	print auth
